package com.bsh.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bsh.dto.MerchantListQryDto;
import com.bsh.mybatis.model.MerchantImg;
import com.bsh.repository.DaoMerchantImg;
import com.bsh.service.MerchantImageService;
import com.bsh.utils.Pager;
import com.bsh.utils.SimpleCache;
import com.mysql.jdbc.StringUtils;

/**
 * 商家展示图片
 * @author gsh
 *
 */
@Service
@Transactional
public class MerchantImageServiceImpl implements MerchantImageService {
	public static SimpleCache<Integer>cache=new SimpleCache<Integer>(1800);
	@Autowired
	private DaoMerchantImg daoMerchantImg;

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantImgService#getPage(com.bsh.utils.Pager, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager,String title,MerchantListQryDto qry) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isNullOrEmpty(title))
			sb.append(String.format(" and u.title = '%s' ",title.trim()));	
		if(qry!=null &&!StringUtils.isNullOrEmpty(qry.getMerchantName()))
			sb.append(String.format(" and m.merchant_name = '%s' ",qry.getMerchantName().trim()));	
		List<Map<String, Object>> list = daoMerchantImg.getPage(pager,sb.toString());
		return list;
	}

	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantImgService#getMerchantImg(java.lang.Integer)
	 */
	@Override
	public MerchantImg getMerchantImg(Integer id) {
		return daoMerchantImg.selectByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantImgService#addMerchantImg(com.bsh.mybatis.model.MerchantImg)
	 */
	@Override
	public Integer addMerchantImg(MerchantImg image) {
		image.setCreateTime(new Date());
		return daoMerchantImg.insert(image);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantImgService#editMerchantImg(com.bsh.mybatis.model.MerchantImg, boolean)
	 */
	@Override
	public Integer editMerchantImg(MerchantImg image,boolean updateImage) {
		MerchantImg imgOld = daoMerchantImg.selectByPrimaryKey(image.getId());
		if(updateImage){
			imgOld.setUrl(image.getUrl());
		}
		imgOld.setSort(image.getSort());
		return daoMerchantImg.updateByPrimaryKey(imgOld);
	}



	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.MerchantImgService#deleteMerchantImg(int)
	 */
	@Override
	public Integer deleteMerchantImg(int id) {
		return daoMerchantImg.deleteByPrimaryKey(id);
	}
	
	@Override
	public List<MerchantImg> getMerchantImgList(Integer merchantId) {
		return daoMerchantImg.getMerchantImgList(merchantId);
	}
}
